package cn.edu.nwsuaf.examcenter.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nwsuaf.examcenter.model.ListModel;
import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.College;
import cn.edu.nwsuaf.examcenter.service.CollegeService;

/****
 * 学院控制器，用于控制学院数据流向以及页面跳转
 * @author Fantacy
 * @version 1.0
 */
@Controller
@RequestMapping(value="/college")
public class CollegeController {
	
	@Resource(name="collegeService")
	private CollegeService collegeService;
	
	/****
	 * 数据导出
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value="/collegeExport")
	public void collegeExport(@RequestParam(value="key", required=false)String key,HttpServletRequest request, HttpServletResponse response){
		collegeService.collegeExport(key, request, response);
	}
	/****
	 * 数据上传（单体上传和批量上传）
	 * @param college
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/collegeAdd",method=RequestMethod.POST)
	public String collegeAdd(@ModelAttribute College college){
		return collegeService.addCollege(college);
	}
	
	/***
	 * 单体删除
	 * @param collegeNo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/collegeDelete/{collegeNo}")
	public String collegeDelete(@PathVariable(value="collegeNo")String collegeNo){
		return collegeService.deleteCollege(collegeNo);
	}
	
	/****
	 * 批量删除
	 * @param str 学院编号串
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/collegeListDelete",method=RequestMethod.POST)
	public String collegeListDelete(@RequestParam("array") String str){
		String[] s1 = str.split(","); //分割学院编号形成学院编号数组
		List<String> list = new ArrayList<>();
		for(String s:s1){
			list.add(s);
		}
		return collegeService.deleteCollegeList(list);
	}
	
	/****
	 * 查询学院
	 * @param key 关键字
	 * @param p 当前页码
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/loadCollege")
	public String loadCollege(@RequestParam(value="key",required=false) String key, @RequestParam(value="p",required=false)String p, ModelMap modelMap){
		collegeService.loadCollege(modelMap);
		return "exam/college-list";
	}
}
